package com.block.api.controller.source;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.source.service.ISourceHandleCheckUserV2Service;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * @Author: ld
 * @Date: 2024-08-26 16:22
 * @Description: 全流程 定制版
 * @Version: 1.0
 **/
@RestController
@RequestMapping("apisource/apis/v2")
public class SourceApiV2DataController {

    @Autowired
    ISourceHandleCheckUserV2Service sourceHandleCheckUserV2Service;

    private static final Logger log = LogFactory.getSourceLog();


    /**
     * 线索全流程撞库
     */
    @RequestMapping(value = "/checkUser/{channelCode}/{receiveType}", method = {RequestMethod.POST})
    public String sourceCheckUser(HttpServletRequest request, HttpServletResponse httpServletResponse,
                                  @PathVariable("channelCode") String channelCode, @PathVariable("receiveType") String receiveType) {

        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            String paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            String ip= ServletUtil.getClientIP(request);
            // 线索api 撞库
            Long startTime = System.currentTimeMillis();
            JSONObject returnObject = sourceHandleCheckUserV2Service.apiCheckUserV2(channelCode, parameterMap, paramContent, ip, receiveType);
            Long endTime = System.currentTimeMillis();
            log.info("【线索api撞库】返回数据展示【channelCode】{}【time】{}【returnObject】{}", channelCode, endTime-startTime, JSON.toJSONString(returnObject));
            return returnObject.toJSONString();
        }catch (Exception exception){
            log.error("撞库异常", exception);
        }
        return "撞库操作异常";
    }

    /**
     * 线索全流程进件
     */
    @RequestMapping(value = "/registerUser/{channelCode}/{receiveType}", method = {RequestMethod.POST})
    public String registerUser(HttpServletRequest request, HttpServletResponse httpServletResponse,
                               @PathVariable("channelCode") String channelCode, @PathVariable("receiveType") String receiveType) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            String paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
            String ip= ServletUtil.getClientIP(request);
            // 线索api 进件
            JSONObject returnObject = sourceHandleCheckUserV2Service.apiRegisterUserV2(channelCode, parameterMap, paramContent, ip, receiveType);
            log.info("【线索api进件】返回数据展示【channelCode】{}【returnObject】{}", channelCode, JSON.toJSONString(returnObject));
            return returnObject.toJSONString();
        }catch (Exception exception){
            log.error("进件异常", exception);
        }
        return "撞库操作异常";
    }
}
